package com.example.gymnasiumsystem.dao;

import com.example.gymnasiumsystem.enity.GymInfo;
import com.example.gymnasiumsystem.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GymInfoDAO {
    // 获取所有体育馆
    public List<GymInfo> getAllGyms() {
        List<GymInfo> gymList = new ArrayList<>();
        String sql = "SELECT * FROM gymInfo";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql);
             ResultSet resultSet = statement.executeQuery()) {

            while (resultSet.next()) {
                GymInfo gymInfo = new GymInfo();
                gymInfo.setGymId(resultSet.getInt("gymId"));
                gymInfo.setGymName(resultSet.getString("gymName"));
                gymInfo.setGymLocation(resultSet.getString("gymLocation"));
                gymInfo.setGymDescription(resultSet.getString("gymDescription"));
                gymList.add(gymInfo);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gymList;
    }
    // 添加分页查询方法
    public List<GymInfo> getGymsByPage(int pageNum, int pageSize) {
        List<GymInfo> gymList = new ArrayList<>();

        // 计算起始位置
        int offset = (pageNum - 1) * pageSize;

        // SQL 查询语句，使用 LIMIT 进行分页
        String sql = "SELECT * FROM gymInfo LIMIT ?, ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {

            // 设置分页参数
            statement.setInt(1, offset);  // 起始位置
            statement.setInt(2, pageSize); // 每页显示数量

            // 执行查询
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    GymInfo gymInfo = new GymInfo();
                    gymInfo.setGymId(resultSet.getInt("gymId"));
                    gymInfo.setGymName(resultSet.getString("gymName"));
                    gymInfo.setGymLocation(resultSet.getString("gymLocation"));
                    gymInfo.setGymDescription(resultSet.getString("gymDescription"));
                    gymList.add(gymInfo);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gymList;
    }

    // 添加体育馆
    public boolean addGym(GymInfo gymInfo) {
        String sql = "INSERT INTO gymInfo (gymName, gymDescription, gymLocation) VALUES (?, ?, ?)";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, gymInfo.getGymName());
            statement.setString(2, gymInfo.getGymDescription());
            statement.setString(3, gymInfo.getGymLocation());
            return statement.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 删除体育馆
    public boolean deleteGym(int gymId) {
        String sql = "DELETE FROM gymInfo WHERE gymId = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, gymId);
            return statement.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 更新体育馆信息（根据名称）
    public boolean updateGymByName(String gymName, GymInfo gymInfo) {
        String sql = "UPDATE gymInfo SET gymName = ?, gymDescription = ?, gymLocation = ? WHERE gymName = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, gymInfo.getGymName());
            statement.setString(2, gymInfo.getGymDescription());
            statement.setString(3, gymInfo.getGymLocation());
            statement.setString(4, gymName);
            return statement.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    // 根据 gymName 查询体育馆
    public GymInfo getGymByName(String gymName) {
        String sql = "SELECT * FROM gymInfo WHERE gymName = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, gymName);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    GymInfo gymInfo = new GymInfo();
                    gymInfo.setGymId(resultSet.getInt("gymId"));
                    gymInfo.setGymName(resultSet.getString("gymName"));
                    gymInfo.setGymDescription(resultSet.getString("gymDescription"));
                    gymInfo.setGymLocation(resultSet.getString("gymLocation"));
                    return gymInfo;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    // 删除体育馆（根据名称）
    public boolean deleteGymByName(String gymName) {
        String sql = "DELETE FROM gymInfo WHERE gymName = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, gymName);
            return statement.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 根据 gymId 查询体育馆
    public GymInfo getGymById(int gymId) {
        String sql = "SELECT * FROM gymInfo WHERE gymId = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, gymId);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    GymInfo gymInfo = new GymInfo();
                    gymInfo.setGymId(resultSet.getInt("gymId"));
                    gymInfo.setGymName(resultSet.getString("gymName"));
                    gymInfo.setGymDescription(resultSet.getString("gymDescription"));
                    gymInfo.setGymLocation(resultSet.getString("gymLocation"));
                    return gymInfo;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 更新体育馆信息
    public boolean updateGym(GymInfo gymInfo) {
        String sql = "UPDATE gymInfo SET gymName = ?, gymDescription = ?, gymLocation = ? WHERE gymId = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, gymInfo.getGymName());
            statement.setString(2, gymInfo.getGymDescription());
            statement.setString(3, gymInfo.getGymLocation());
            statement.setInt(4, gymInfo.getGymId());
            return statement.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
